import { Controller, Get, Post, ApiInfo, JwtUnless } from '../../../../libs/koa-action-decorator/index'
import { AdminUser, verifiAdminUser } from '../models/userModel'
import { adminLogin, adminRegister, getAdminInfo, getAdminPage, adminUpdate } from '../services/userService'
import svgCaptcha from 'svg-captcha'
import { ErrorModel, SuccessModel } from '../../../../utils/respond/ResModel'
import { deleteByIds } from '../../../../db/util'
export default @ApiInfo({ name: '后台用户接口', catalogue: '用户管理' })
@Controller('/api/admin/user')
class userControllers {
    @Get('/vcode')
    @ApiInfo('获取登录验证码')
    @JwtUnless
    async vcode(ctx) {
        const cap = svgCaptcha.create({
            size: 4, // 验证码长度
            width: 126,
            height: 34,
            fontSize: 42,
            ignoreChars: '0oO1ilI', // 验证码字符中排除 0o1i
            noise: 2 // 干扰线条的数量
        })
        let token = ctx.setJwtSession({ captcha: cap.text }, 60) // 一分钟过期
        ctx.body = new SuccessModel({ vcode: cap.data, token })
    }

    @Get('/verifyVcode')
    @ApiInfo('校验登录验证码')
    async verifyVcode(ctx) {
        try {
            const vcode = ctx.getQueryString('vcode').toLowerCase();
            let captcha = ctx.jwtSession.captcha.toLowerCase()
            let verifyPass = vcode === captcha;
            ctx.body = verifyPass ? new SuccessModel({ verifyPass }) : new ErrorModel({ message: '校验失败，验证码错误或过期' })
        } catch (error) {
            ctx.body = new ErrorModel({ message: '发生错误' })
        }
    }


    @Post('/login')
    @JwtUnless
    @ApiInfo('后台用户登录')
    async adminLogin(ctx) {
        const body = ctx.request.body;
        const { checkSuccess, checkResult } = verifiAdminUser(body, ['userName']);
        ctx.body = checkSuccess && await adminLogin(body, ctx) || checkResult;
    }

    @Get('/info')
    @JwtUnless
    @ApiInfo('后台用户获取个人信息')
    async getAdminInfo(ctx) {
        const { userId } = ctx.jwtSession;
        ctx.body = await getAdminInfo(userId);
    }


    @Post('/register')
    @ApiInfo('添加后台用户')
    async adminRegister(ctx) {
        const body = ctx.request.body;
        const { checkSuccess, checkResult } = verifiAdminUser(body);
        ctx.body = checkSuccess && await adminRegister(body) || checkResult;
    }


    @Post('/update')
    @ApiInfo('修改后台用户')
    async adminUpdate(ctx) {
        const body = ctx.request.body;
        const { checkSuccess, checkResult } = verifiAdminUser(body);
        ctx.body = checkSuccess && await adminUpdate(body) || checkResult;
    }


    @Post('/delete')
    @ApiInfo('删除后台用户')
    async adminDelete(ctx) {
        const body = ctx.request.body;
        const { ids } = body;
        let result = await deleteByIds(AdminUser, ids)
        ctx.body = new SuccessModel({ message: result });
    }

    @Get('/page')
    @ApiInfo('分页获取用户列表')
    async getAdminPage(ctx) {
        const page = ctx.getQueryNumber('page', 1);
        const size = ctx.getQueryNumber('size', 20);
        ctx.body = await getAdminPage({ page, size });
    }

}